//
//  EvalutionTeacherController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/12.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "EvalutionTeacherController.h"
#import "UserInfoModel.h"

@interface EvalutionTeacherController ()
// 视图
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *stars;       // 星星
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;                           // 星星评分
@property (weak, nonatomic) IBOutlet UITextView *textView;                          // 内容
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;                      // 滚动层

// 数据
@property (nonatomic, retain)TeacherInfoModel *model;          // 导师模型


@end

@implementation EvalutionTeacherController



- (void)viewDidLoad {
    [super viewDidLoad];
    // 手动调整View
    self.automaticallyAdjustsScrollViewInsets = false;
    self.title = @"评价导师";
    self.view.backgroundColor = [UIColor lightGrayColor];

    // 获取导师相关信息
    [self updateTeacherInfo];
    // 设置视图
    [self setupView];
}

#pragma mark - 网络请求
-(void)updateTeacherInfo {
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getTeacherInfo"
                                        parameters:@{@"s_id": [UserInfoModel shareInstance].stu_id, @"tea_id": self.teacherID}
                                           success:^(NSDictionary *respondDictionary) {
                                               self.model = [[TeacherInfoModel alloc] initWithDictionary:respondDictionary[@"teacherInfo"]];
                                               // 设置header模型
                                               self.headerView.model = self.model;

                                           }
                                           failure:^(NSError *error) {
                                               
                                           }];



}

#pragma mark - 自定义方法
-(void)setupView {
    // 设置header数据
    self.headerView.model = self.model;
    
    // 设置星星点击事件
    for (UIImageView *imageView in self.stars) {
        imageView.userInteractionEnabled = true;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(starTapped:)];
        [imageView addGestureRecognizer:tap];
    }
    
    // 清空textView内容
    self.textView.text = @"";
    
    // 监听键盘,调整输入
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

-(void)starTapped:(UITapGestureRecognizer *)sender {
    // 星星在数组中位置
    NSInteger index = [self.stars indexOfObject:sender.view];
    
    // 将index前(包括index)的星星点亮
    for (int i = 0; i < self.stars.count; i++) {
        if (i <= index) {
            // 如果在index前则设置为点亮
            UIImageView *imageView = (UIImageView *)self.stars[i];
            imageView.image = [UIImage imageNamed:@"star_orange"];
        }else {
            // 如果在index后则设置为变暗
            UIImageView *imageView = (UIImageView *)self.stars[i];
            imageView.image = [UIImage imageNamed:@"star_gray"];
        }
        
    }
    
    // 改变label现实数字
    self.scoreLabel.text = [NSString stringWithFormat:@"%d分",index+1];
}
#pragma mark - 事件监听
- (IBAction)submitTapped:(id)sender {
    // 请求参数
    NSDictionary *parameters = @{@"yy_id": @(self.yyid), @"s_id": [UserInfoModel shareInstance].stu_id, @"tea_star": @([self.scoreLabel.text intValue]), @"tea_comit": self.textView.text};
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/pingjiaTeacher"
                                        parameters:parameters
                                           success:^(NSDictionary *respondDictionary) {
                                               if([respondDictionary[@"code"] intValue] == 1){
                                                   [SVProgressHUD showSuccessWithStatus:@"评价成功"];
                                                   [self.navigationController popViewControllerAnimated:true];
                                               } else {
                                                   [SVProgressHUD showErrorWithStatus:@"评价失败"];
                                               }
                                           } failure:^(NSError *error) {
                
                [SVProgressHUD showfailed];
                                           }];
}

// 键盘即将出现
-(void)keyboardWillShow:(NSNotification*)notification{
    if (self.scrollView.contentOffset.y == 0) {
        // 如果没有滚动,则向上调整UI
        [self.scrollView setContentOffset:CGPointMake(0, 70) animated:true];
    }
    
}

// 键盘将隐藏
-(void)keyboardWillHide:(NSNotification*)notification{
    //调整回UI
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:true];
}

#pragma mark - 重写父类方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textView resignFirstResponder];
}



@end
